package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bpo.RoomBpo;
import bpo.RoomLogBpo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import dao.RoomDao;



/**
 * Servlet implementation class RoomServlet
 */
@WebServlet("/RoomLogServlet")
public class RoomLogServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("action");
        if(action.equals("query")){
            this.query(request, response);
        }else if(action.equals("searchbyfr")){
            this.searchRoomByfr(request, response);
        }else{
            request.getRequestDispatcher("/error.jsp").forward(request, response);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request,response);
    }

    private void query(HttpServletRequest request, HttpServletResponse response) throws IOException{
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        list = RoomLogBpo.getAllRoomLog();
        Object jsonArray = JSON.toJSON(list);
        JSONObject result = new JSONObject();
        result.put("rows", jsonArray);
        result.put("total", list.size());
        response.getWriter().print(result);
    }
    private void searchRoomByfr(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String fno = request.getParameter("fno");
        String rno = request.getParameter("rno");
        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
        list = RoomLogBpo.searchListByfr(fno, rno);
        Object jsonArray = JSON.toJSON(list);
        JSONObject result = new JSONObject();
        result.put("rows", jsonArray);
        result.put("total", list.size());
        response.getWriter().print(result);
    }

}

